<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" class="data-form">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="会员账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          value-format = "yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.time"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="统计信息">
        <template slot-scope="scope">
          <span>{{userType[scope.$index]}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="dataList"
        header-align="center"
        align="center"
        label="数量">
        <template slot-scope="scope">
            <span>{{dataList[scope.$index]}}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="count2Level(scope.$index)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      class="two-dialog"
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-table
        :data="data2List"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          prop="type"
          header-align="center"
          align="center"
          label="统计信息">
          <template slot-scope="scope">
            <span>{{upgradeType[scope.$index]}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="num"
          header-align="center"
          align="center"
          label="数量">
          <template slot-scope="scope">
            <span>{{data2List[scope.$index]}}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="upgradeOpen(scope.$index)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog
      title="推广详情"
      :visible.sync="upgradeVisible"
      width="80%">
      <upgrade ref="upgrade" @refreshDataList="getDataList"></upgrade>
    </el-dialog>
  </div>
</template>

<script>
  import {countNameList2Level, countNameList, upgradeType} from '@/utils/countType'
  import {userType} from '@/utils/userType'
  import Upgrade from './upgrade'
  export default {
    data () {
      return {
        dataForm: {
          time: new Date(),
          keywords: '',
          type1: ''
        },
        countNameList: countNameList,
        countNameList2Level: countNameList2Level,
        upgradeType: upgradeType,
        userType: userType,
        dataList: [],
        data2List: [],
        dataListLoading: false,
        dataListSelections: [],
        dialogVisible: false,
        upgradeVisible: false
      }
    },
    components: {
      Upgrade
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/member/upgrade/queryExtensionList'),
          method: 'get',
          params: this.$http.adornParams({
            'getUserType': '',
            'lastStart': this.dataForm.time[0],
            'lastEnd': this.dataForm.time[1],
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data
            // this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      count2Level (id) {
        this.dataForm.type1 = id
        this.data2List = []
        this.dialogVisible = true
        this.$http({
          url: this.$http.adornUrl('/member/upgrade/queryUpgradeType'),
          method: 'get',
          params: this.$http.adornParams({
            'getUserType': id + 1,
            'lastStart': this.dataForm.time[0],
            'lastEnd': this.dataForm.time[1],
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.data2List = data.data
          } else {
            this.data2List = []
          }
          this.dataListLoading = false
        })
      },
      upgradeOpen (id) {
        this.upgradeVisible = true
        this.$nextTick(() => {
          this.$refs.upgrade.getDataList(id, this.dataForm.type1)
        })
      }
    }
  }
</script>
<style>
  .characters{
    width:300px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
  .el-dialog__body {
    height: 600px;
    overflow-y: scroll;
  }
</style>
